package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.entities.login.ShareWeightRuleEntity;
import com.maiji.cloud.entities.login.WeightChangeLog;
import com.maiji.cloud.mapper.WeightChangeLogMapper;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.service.WeightChangeLogService;
import com.maiji.cloud.utils.CommonIntValueEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class WeightChangeLogServiceImpl extends ServiceImpl<WeightChangeLogMapper, WeightChangeLog> implements WeightChangeLogService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public BaseDataResDto<List<ShareWeightRuleEntity>> findAllShareWeightRuleLogs(List<Date> updateDates) {
        Query query = new Query(Criteria.where("type").is(CommonIntValueEnum.MONGO_RULE_TYPE_REDUCE_WEIGHT_BASE_FUND.getValue()));
        query.skip(1).with(new Sort(new Sort.Order(Sort.Direction.DESC,"updateDate")));
        Date startDate = updateDates.size() > 0 ? updateDates.get(0): null;
        Criteria updateDateCriteria = Criteria.where("updateDate");
        Boolean updFlag = false;
        if (startDate != null) {
            updateDateCriteria = updateDateCriteria.gte(startDate);
            updFlag = true;
        }
        Date endDate = updateDates.size() > 1 ? updateDates.get(1): null;
        if (endDate != null) {
            updateDateCriteria = updateDateCriteria.lte(endDate);
            updFlag = true;
        }
        if (updFlag) query.addCriteria(updateDateCriteria);
        if (startDate == null && endDate == null) query.limit(1);
        List<ShareWeightRuleEntity> shareWeightHelpRules = mongoTemplate.find(query, ShareWeightRuleEntity.class, ShareWeightRuleEntity.TABLE_LOG_NAME);
        return new BaseDataResDto<List<ShareWeightRuleEntity>>(Status.SUCCESS).setData(shareWeightHelpRules);
    }

    @Override
    public BaseResDto updShareWeightRule(ShareWeightRuleEntity shareWeightRuleEntity) {
        Query query = new Query(Criteria.where("type").is(CommonIntValueEnum.MONGO_RULE_TYPE_REDUCE_WEIGHT_BASE_FUND.getValue()));
        mongoTemplate.findAndRemove(query, ShareWeightRuleEntity.class, ShareWeightRuleEntity.TABLE_NAME);
        mongoTemplate.save(shareWeightRuleEntity, ShareWeightRuleEntity.TABLE_NAME);
        mongoTemplate.save(shareWeightRuleEntity, ShareWeightRuleEntity.TABLE_LOG_NAME);
        return new BaseResDto(Status.SUCCESS);
    }

    @Override
    public BaseDataResDto<ShareWeightRuleEntity> findShareWeightRule() {
        Query query = new Query(Criteria.where("type").is(CommonIntValueEnum.MONGO_RULE_TYPE_REDUCE_WEIGHT_BASE_FUND.getValue()));
        ShareWeightRuleEntity shareWeightRuleEntity = mongoTemplate.findOne(query, ShareWeightRuleEntity.class, ShareWeightRuleEntity.TABLE_NAME);
        return new BaseDataResDto<ShareWeightRuleEntity>(Status.SUCCESS).setData(shareWeightRuleEntity);
    }

}
